/**
  ******************************************************************************
  * @file           : bsp.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BSP_H
#define __BSP_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"

#include "stm32f4xx_ll_rcc.h"
#include "stm32f4xx_ll_bus.h"
#include "stm32f4xx_ll_system.h"
#include "stm32f4xx_ll_exti.h"
#include "stm32f4xx_ll_cortex.h"
#include "stm32f4xx_ll_utils.h"
#include "stm32f4xx_ll_pwr.h"
#include "stm32f4xx_ll_dma.h"
#include "stm32f4xx_ll_spi.h"
#include "stm32f4xx_ll_tim.h"
#include "stm32f4xx_ll_usart.h"
#include "stm32f4xx_ll_gpio.h"
#include "stm32f4xx_ll_rtc.h"

//#define MODBUS_UART_IRQ_PRIORITY            6
//#define MODBUS_TIMEOUT_IRQ_PRIORITY         7
//#define MODBUS_TXDMA_IRQ_PRIORITY           7
//#define ICS220J_UART_IRQ_PRIORITY           8
//#define ICS220J_TIMEOUT_IRQ_PRIORITY        8
//#define ICS220J_TXDMA_IRQ_PRIORITY          8
#define MEAS_TIMETICK_IRQ_PRIORITY            8
#define SENSOR_UART_IRQ_PRIORITY              8
#define SENSOR_OUTTIMER_IRQ_PRIORITY          8
#define SENSOR_UART_txDMA_IRQ_PRIORITY        8
#define FIBER_UART_IRQ_PRIORITY               8
#define FIBER_OUTTIMER_IRQ_PRIORITY           8
#define DCLCD_UART_IRQ_PRIORITY               8
#define DCLCD_OUTTIMER_IRQ_PRIORITY           8
#define DCLCD_UART_txDMA_IRQ_PRIORITY         8
#define RS485N1_UART_IRQ_PRIORITY             8
#define RS485N1_OUTTIMER_IRQ_PRIORITY         8
#define RS232N1_UART_IRQ_PRIORITY             8
#define RS232N1_OUTTIMER_IRQ_PRIORITY         8
#define SDIO_IRQ_PRIORITY                     8
#define IRKEY_TIMER_IRQ_PRIORITY              8
#define HLKB35_UART_IRQ_PRIORITY              8
#define HLKB35_OUTTIMER_IRQ_PRIORITY          8
#define ETHERNET_SPI_IRQ_PRIORITY             8
#define ETHERNET_EXIT_IRQ_PRIORITY            8
//#define MEAS_TIMETICK_IRQ_PRIORITY          9
//#define AO420MA_TIMER_IRQ_PRIORITY          8

/* Private defines -----------------------------------------------------------*/
#define LED_Pin                  GPIO_PIN_2
#define LED_LL_Pin               LL_GPIO_PIN_2
#define LED_GPIO_Port            GPIOB
#define Led_on()                 (LED_GPIO_Port->BSRR = (uint32_t)LED_Pin)
#define Led_off()                (LED_GPIO_Port->BRR = (uint32_t)LED_Pin)
#define Led_toggle()             LL_GPIO_TogglePin(LED_GPIO_Port, LED_LL_Pin)
#define Led(x)                   x?(LED_GPIO_Port->BSRR = (uint32_t)LED_Pin):(LED_GPIO_Port->BSRR = ((uint32_t)LED_Pin << 16U))

#define FIBER_TX_Pin LL_GPIO_PIN_2
#define FIBER_TX_GPIO_Port GPIOA
#define FIBER_RX_Pin LL_GPIO_PIN_3
#define FIBER_RX_GPIO_Port GPIOA

#define AO420MA1_Pin LL_GPIO_PIN_0
#define AO420MA1_GPIO_Port GPIOB
#define AO420MA2_Pin LL_GPIO_PIN_1
#define AO420MA2_GPIO_Port GPIOB

#define DI1_Pin LL_GPIO_PIN_9
#define DI1_GPIO_Port GPIOE
#define DI2_Pin LL_GPIO_PIN_10
#define DI2_GPIO_Port GPIOE
#define DI3_Pin LL_GPIO_PIN_11
#define DI3_GPIO_Port GPIOE
#define DI4_Pin LL_GPIO_PIN_12
#define DI4_GPIO_Port GPIOE
#define DO4_Pin LL_GPIO_PIN_13
#define DO4_GPIO_Port GPIOE
#define DO3_Pin LL_GPIO_PIN_14
#define DO3_GPIO_Port GPIOE
#define DO2_Pin LL_GPIO_PIN_15
#define DO2_GPIO_Port GPIOE
#define DO1_Pin LL_GPIO_PIN_10
#define DO1_GPIO_Port GPIOB
#define RS232_1_RX_Pin LL_GPIO_PIN_12
#define RS232_1_RX_GPIO_Port GPIOB
#define RS232_1_TX_Pin LL_GPIO_PIN_13
#define RS232_1_TX_GPIO_Port GPIOB
#define LCD_TX_Pin LL_GPIO_PIN_8
#define LCD_TX_GPIO_Port GPIOD
#define LCD_RX_Pin LL_GPIO_PIN_9
#define LCD_RX_GPIO_Port GPIOD

#define SYS_VERSION_SN_MXLEN                16
#define SYS_VERSION_SV_MXLEN                16
#define SYS_VERSION_PV_MXLEN                16
#define SYS_VERSION_BD_MXLEN                20

typedef struct
{
  const char sn[SYS_VERSION_SN_MXLEN];
  const char sv[SYS_VERSION_SV_MXLEN];
  const char pv[SYS_VERSION_PV_MXLEN];
  const char uv[SYS_VERSION_PV_MXLEN];
  const char bd[SYS_VERSION_BD_MXLEN];
}Sys_VersionStru;

#define SYS_RTC_YEAR_BASE                   2020u

#define BIN2BCD(__VALUE__) (uint8_t)((((__VALUE__) / 10u) << 4u) | ((__VALUE__) % 10u))
#define BCD2BIN(__VALUE__) (uint8_t)(((uint8_t)((__VALUE__) & (uint8_t)0xF0u) >> (uint8_t)0x4u) * 10u + ((__VALUE__) & (uint8_t)0x0Fu))

typedef struct _Sys_DataTimeStru
{
  uint8_t a0m1;     /* 用于保存累计清零手自动方式的变量 */
  uint8_t effective;
  uint8_t year;     /* (2000~2099)-2000 */
  uint8_t month;
  uint8_t day;
  uint8_t hour;
  uint8_t minute;
  uint8_t second;
}Sys_DataTimeStru;

extern void system_init(void);
extern void sys_rtc_init(void);
extern uint8_t sys_check_datatime(const Sys_DataTimeStru *ckdt);
extern void sys_set_datatime(const Sys_DataTimeStru *adjust);
extern void rs232n1_uart_init();
extern uint16_t sys_crc16(const uint8_t *buf, uint32_t len);
extern uint16_t sys_crc16ex(uint16_t init, const uint8_t *buf, uint32_t len);

extern const Sys_VersionStru sys_version;
extern Sys_DataTimeStru sys_datatime;

/* debug macro */
#if (SYS_DEBUG_LEVEL > 0)
  #define  SysInfo(...)    _printf(__VA_ARGS__);
#else
  #define SysInfo(...)   
#endif 
                            
#if (SYS_DEBUG_LEVEL > 1)                         
  #define  SysDbgLog(...)    printf(__VA_ARGS__);//_printf("[D]:") ;                           
#else
  #define SysDbgLog(...)                         
#endif

#ifdef SYS_ERRLOG
  #define  SysErrLog(...)    _printf("[E] '%s' '%s' '%d':\r\n", __FILE__, __FUNCTION__, __LINE__) ;\
                             _printf(__VA_ARGS__);
#else
  #define SysErrLog(...)   
#endif 

#ifdef __cplusplus
}
#endif

#endif /* __BSP_H */
